<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Household;
use Illuminate\Http\Request;

class OwnerController extends Controller
{
    public function index()
    {

    }

    /*
     * 列表信息
     * */
    public function lists(Request $request, Household $household)
    {
        if (empty($request->condition)) {
            $page = ($request->page - 1) * $request->limit;
            $res = $household->offset($page)->limit($request->limit)->get()->toArray();
            return $this->return_json(200, $res, '业主信息');
        } else {
            $page = ($request->page - 1) * $request->limit;
            $door_number = str_replace('-', '', $request->condition);//去掉多余字符
            $res = $household->wheredoor_number($door_number)->offset($page)->limit($request->limit)->get()->toArray();
            return $this->return_json(200, $res, '业主信息');
        }
    }

    /*
     * 页数
     * */
    public function total(Request $request, Household $household)
    {
        if (empty($request->condition)) {
            $total = $household->paginate()->total();
            return $this->return_json(200, $total, '总条数数');
        } else {
            $door_number = str_replace('-', '', $request->condition);//去掉多余字符
            $total = $household->wheredoor_number($door_number)->paginate()->total();
            return $this->return_json(200, $total, '总条数数');
        }
    }

    /*
     * 添加业主
     * */
    public function save(Request $request, Household $household)
    {
        $this->validate($request,
            ['door_number' => 'required'],
            ['door_number.required' => '门牌号不能为空']);
        $door_number = str_replace('-', '', $request->door_number);//去掉多余字符
//        $res  =  $household->wheredoor_number($door_number)->get()-
        $data = $request->all();
        $img = $request->file('just_idk');
        $imgs = $request->file('back_idk');
        if (!empty($img)) {
            if ($img->isValid()) {
                //获取文件名自带后缀
                $imageName = $img->getClientOriginalName();
                //避免重名
                $time = time();
                $t = date("YmdHms_", $time);
                $name = $t . $imageName;
                //上传定义路径
                $dir = env("UPLOAD_DIR");//.Env 里配置路径
                $img->move($dir, $name);
            }
        }
        if (!empty($imgs)) {
            if ($imgs->isValid()) {
                //获取文件名自带后缀
                $imageNames = $imgs->getClientOriginalName();
                //避免重名
                $times = time();
                $ts = date("YmdHms_", $times);
                $names = $ts . $imageNames;
                //上传定义路径
                $dir = env("UPLOAD_DIR");//.Env 里配置路径
                $imgs->move($dir, $names);
            }
        }
        $data['just_idk'] = empty($name) ? '' : 'img/' . $name;
        $data['back_idk'] = empty($names) ? '' : 'img/' . $names;
        unset($data['door_number']);
        $res = $household->wheredoor_number($door_number)->update($data);
        if ($res) {
            return $this->return_json(200, '', '添加成功');
        } else {
            return $this->return_json(201, '', '添加失败');
        }
    }

    /*
     * 修改信息
     * */
    public function update_info(Request $request, Household $household)
    {
        $res = $household->whereid($request->id)->get()->toArray();
        return $this->return_json(200, $res, '业主修改信息');
    }

    /*
     * 删除住户信息
     * */
    public function delete(Request $request, Household $household)
    {
        $data = [
            'owner_name' => '', 'owner_phone' => '', 'title_number' => '', 'area' => '',
            'number_residents' => '', 'status' => '', 'renter_name' => '', 'tenant_phone' => '', 'lease_term' => '',
            'just_idk' => '', 'back_idk' => '',
        ];
        $res = $household->whereid($request->id)->update($data);
        if ($res) {
            return $this->return_json(200, '', '删除成功');
        } else {
            return $this->return_json(201, '', '删除失败');
        }
    }

    /*
     * 修改业主
     * */
    public function update(Request $request, Household $household)
    {
        $this->validate($request,
            ['door_number' => 'required'],
            ['door_number.required' => '门牌号不能为空']);
        $data = $request->all();
        $door_number = str_replace('-', '', $request->door_number);//去掉多余字符
        $img = $request->file('just_idk');
        $imgs = $request->file('back_idk');
        if (!empty($img)){
            if ($img->isValid()) {
                //获取文件名自带后缀
                $imageName = $img->getClientOriginalName();
                //避免重名
                $time = time();
                $t = date("YmdHms_", $time);
                $name = $t . $imageName;
                //上传定义路径
                $dir = env("UPLOAD_DIR");//.Env 里配置路径
                $img->move($dir, $name);
            }
        }
        if (!empty($imgs)) {
            if ($imgs->isValid()) {
                //获取文件名自带后缀
                $imageNames = $imgs->getClientOriginalName();
                //避免重名
                $times = time();
                $ts = date("YmdHms_", $times);
                $names = $ts . $imageNames;
                //上传定义路径
                $dir = env("UPLOAD_DIR");//.Env 里配置路径
                $imgs->move($dir, $names);
            }
        }
        $data['just_idk'] = empty($name) ?  $request->img_urlz : 'img/' . $name;
        $data['back_idk'] = empty($names) ? $request->img_urlf : 'img/' . $names;
        unset($data['door_number']); unset($data['img_urlz']);unset($data['img_urlf']);
        $res = $res = $household->wheredoor_number($door_number)->update($data);
        if ($res) {
            return $this->return_json(200, '', '修改成功');
        } else {
            return $this->return_json(201, '', '修改失败');
        }
    }
}